import xml.parsers.expat
import commands
import os
##@author Alessio Di Pietro
#@author Irene Scionti
#@brief Parser for vsync settings
class Settings(object):
    '''
    Parse versionSync settings from an xml file
    '''
    svnWorkingCopy=''
    diffworkingcopy=''
    contacts_diffname=''
    memos_diffname=''
    calendar_diffname=''
    svnServer=''
    svnRepoName=''
    svnUser=''
    svnPassword=''
    icspath=''
    contactspath=''
    memospath=''
    versiontimer=''
    
    ## @param settingsFilePath path to settings.xml file
    def __init__(self,settingsFilePath):
        '''
        Constructor
        '''
        self.xmlparser=xml.parsers.expat.ParserCreate();
        self.settingsFile=open(settingsFilePath,'r');
        #retrieve user home directory
        self.user_home=commands.getoutput("cat /etc/passwd | grep `whoami`| awk 'BEGIN { FS = \":\" } ;{print $6}'")
    
    def start_element(self,name,attrs):
        '''
        Detects an xml opening tag
        '''
        if name=='svnworkingcopy':
            self.svnWorkingCopy='true';
        elif name=='tmpworkingcopy':
            self.tmpworkingcopy='true';
        elif name=='contacts_diffname':
            self.contacts_diffname='true'
        elif name=='calendar_diffname':
            self.calendar_diffname='true'
        elif name=='memos_diffname':
            self.memos_diffname='true'
        elif name=='svnserver':
            self.svnServer='true';
        elif name=='svnreponame':
            self.svnRepoName='true';
        elif name=='svnuser':
            self.svnUser='true';
        elif name=='svnpassword':
            self.svnPassword='true';
        elif name=='icspath':
            self.icspath='true';
        elif name=='contactspath':
            self.contactspath='true';
        elif name=='memospath':
            self.memospath='true';
        elif name=='versiontimer':
            self.versiontimer='true'
        elif name=='fts_db':
            self.fts_db='true';
        elif name=='fts_db_window_size':
            self.fts_db_window_size='true';
        elif name=='garbage_year_threshold':
            self.garbage_year_threshold='true';
        elif name=='garbage_size_threshold':
            self.garbage_size_threshold='true';
        
        
        
    def char_data(self,data):
        '''
        Detects xml content tag
        '''
        if self.svnWorkingCopy=='true':
            self.svnWorkingCopy=self.user_home+data;
        elif self.tmpworkingcopy=='true':
            self.tmpworkingcopy=data;
        elif self.calendar_diffname=='true':
            self.calendar_diffname=data;
        elif self.contacts_diffname=='true':
            self.contacts_diffname=data;
        elif self.memos_diffname=='true':
            self.memos_diffname=data;  
        elif self.svnServer=='true':
            self.svnServer=data;
        elif self.svnRepoName=='true':
            self.svnRepoName=data;
        elif self.svnUser=='true':
            self.svnUser=data;
        elif self.svnPassword=='true':
            self.svnPassword=data;
        elif self.icspath=='true':
            self.icspath=self.user_home+data;
        elif self.contactspath=='true':
            self.contactspath=self.user_home+data;
        elif self.memospath=='true':
            self.memospath=self.user_home+data;
        elif self.versiontimer=='true':
            self.versiontimer=data;
        elif self.fts_db=='true':
            self.fts_db=data;
        elif self.fts_db_window_size=='true':
            self.fts_db_window_size=data;
        elif self.garbage_year_threshold=='true':
            self.garbage_year_threshold=data;
        elif self.garbage_size_threshold=='true':
            self.garbage_size_threshold=data;
            
  
    def parseSettings(self):
        '''
        Parse settings from xml file
        '''
        self.xmlparser.StartElementHandler=self.start_element;
        self.xmlparser.CharacterDataHandler=self.char_data;
        self.xmlparser.ParseFile(self.settingsFile);
        